package com.xiaobai.zhuangxiuguanjia.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.ViewHolder;
import com.xiaobai.zhuangxiuguanjia.activities.LoginActivity;
import com.xiaobai.zhuangxiuguanjia.activities.MyToolBoxActivity;
import com.xiaobai.zhuangxiuguanjia.activities.MydemandActivity;
import com.xiaobai.zhuangxiuguanjia.activities.SettingsActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment
        implements View.OnClickListener, AdapterView.OnItemClickListener {

    private static final int[] itemImgs = new int[]{R.mipmap.icon_mydemand, R.mipmap.icon_mycollect,
            R.mipmap.icon_my_tool, R.mipmap.icon_mysetting};

    private static final int[] itemTitles = new int[]{R.string.mydemand, R.string.mycollect,
            R.string.mytool, R.string.mysettings};

    private static final int[] itemDigests = new int[]{R.string.mydemand_digest, R.string.mycollect_digest,
            R.string.mytool_digest, R.string.mysettings_digest};

    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ShareSDK.initSDK(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        ImageView headPhoto = (ImageView) view.findViewById(R.id.mine_head_photo);
        headPhoto.setOnClickListener(this);

        ListView mineItemList = (ListView) view.findViewById(R.id.mine_list);
        List<Map<String, Integer>> itemList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Map<String, Integer> map = new HashMap<>();
            map.put("itemImg", itemImgs[i]);
            map.put("itemTitle", itemTitles[i]);
            map.put("itemDigest", itemDigests[i]);

            itemList.add(map);
        }
        MyAdapter adapter = new MyAdapter(itemList);
        mineItemList.setAdapter(adapter);

        mineItemList.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        // TODO 检查是否已经登陆

        // TODO 转跳登陆界面
        switch (v.getId()) {
            case R.id.mine_head_photo:
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
                if (!qq.isValid() && !sina.isValid()) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().overridePendingTransition(R.anim.activity_zoom_in, 0);
                }
                break;
        }

    }

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p/>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent   The AdapterView where the click happened.
     * @param view     The view within the AdapterView that was clicked (this
     *                 will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id       The row id of the item that was clicked.
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                startActivity(new Intent(getActivity(), MydemandActivity.class));
                getActivity().overridePendingTransition(R.anim.activity_zoom_in, R.anim.zoom_out);
                break;
            case 1:
                // TODO 检查是否已经登陆
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
                if (!qq.isValid() && !sina.isValid()) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().overridePendingTransition(R.anim.activity_zoom_in, 0);
                }

                // TODO 否则，获取用户收藏列表
                break;
            case 2:
                startActivity(new Intent(getActivity(), MyToolBoxActivity.class));
                getActivity().overridePendingTransition(R.anim.activity_zoom_in, 0);
                break;
            case 3:
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                getActivity().overridePendingTransition(R.anim.activity_zoom_in, 0);
                break;
        }
    }

    private class MyAdapter extends BaseAdapter {

        private List<Map<String, Integer>> itemList;

        public MyAdapter(List<Map<String, Integer>> itemList) {
            this.itemList = itemList;
        }

        /**
         * How many items are in the data set represented by this Adapter.
         *
         * @return Count of items.
         */
        @Override
        public int getCount() {
            if (itemList != null) {
                return itemList.size();
            }
            return 0;
        }

        /**
         * Get the data item associated with the specified position in the data set.
         *
         * @param position Position of the item whose data we want within the adapter's
         *                 data set.
         * @return The data at the specified position.
         */
        @Override
        public Object getItem(int position) {
            if (itemList != null) {
                return itemList.get(position);
            }
            return null;
        }

        /**
         * Get the row id associated with the specified position in the list.
         *
         * @param position The position of the item within the adapter's data set whose row id we want.
         * @return The id of the item at the specified position.
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * Get a View that displays the data at the specified position in the data set. You can either
         * create a View manually or inflate it from an XML layout file. When the View is inflated, the
         * parent View (GridView, ListView...) will apply default layout parameters unless you use
         * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
         * to specify a root view and to prevent attachment to the root.
         *
         * @param position    The position of the item within the adapter's data set of the item whose view
         *                    we want.
         * @param convertView The old view to reuse, if possible. Note: You should check that this view
         *                    is non-null and of an appropriate type before using. If it is not possible to convert
         *                    this view to display the correct data, this method can create a new view.
         *                    Heterogeneous lists can specify their number of view types, so that this View is
         *                    always of the right type (see {@link #getViewTypeCount()} and
         *                    {@link #getItemViewType(int)}).
         * @param parent      The parent that this view will eventually be attached to
         * @return A View corresponding to the data at the specified position.
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null)
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.mine_item_list_layout, parent, false);

            ImageView itemImg = ViewHolder.getView(convertView, R.id.mine_item_img);
            TextView itemTitle = ViewHolder.getView(convertView, R.id.mine_item_title);
            TextView itemDigest = ViewHolder.getView(convertView, R.id.mine_item_digest);

            Map<String, Integer> map = itemList.get(position);
            itemImg.setImageResource(map.get("itemImg"));
            itemTitle.setText(map.get("itemTitle"));
            itemDigest.setText(map.get("itemDigest"));

            return convertView;
        }
    }
}
